import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_page/main_page/provider/msg_provider.dart';
import 'package:xvideos/hj_utils/date_time_util.dart';
import 'package:xvideos/hj_utils/holiday_theme.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';

import '../../../tools_base/global_store/store.dart';
import '../mine_vip/mine_charge_vip_page.dart';

//信任倒计时
class VipTimeAcView extends StatefulWidget {
  const VipTimeAcView({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _VipTimeAcView();
  }
}

class _VipTimeAcView extends State<VipTimeAcView> {
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => Get.to(() => MineChargeVipPage()),
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16),
        child: Stack(
          alignment: Alignment.centerLeft,
          children: [
            Image.asset(
              HolidayTheme.theme.vipBg,
              width: double.infinity,
              fit: BoxFit.fill,
              height: 35,
            ),
            Consumer<GlobalStore>(
              builder: (context, global, child) => Consumer<MineMsgProvider>(
                builder: (context, provider, child) {
                  if (global.isVIP()) {
                    return _buildVip(global);
                  } else {
                    if ((provider.countdown?.leftTimeSinceEpoch ?? 0) > 0) {
                      return _buildNewer(provider);
                    } else {
                      return _buildNormal();
                    }
                  }
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  _buildNewer(MineMsgProvider provider) {
    String text =
        '新人限时特惠 倒计时 ${provider.countdown?.hourDesc ?? '00'}:${provider.countdown?.minDesc ?? '00'}:${provider.countdown?.secondsDesc ?? '00'}';
    return Row(
      children: [
        90.sizeBoxW,
        Expanded(
          child: Text(
            text,
            style: TextStyle(color: Colors.white, fontSize: 11, fontWeight: FontWeight.w600),
          ),
        ),
        ShaderMask(
          shaderCallback: (Rect bounds) {
            return LinearGradient(
              colors: [Color(0xffE3B284), Color(0xffFEEEDE)],
              tileMode: TileMode.mirror,
            ).createShader(bounds);
          },
          blendMode: BlendMode.srcATop,
          child: Center(
            child: Text(
              '升级会员',
              style: TextStyle(fontSize: 10),
            ),
          ),
        ),
        4.sizeBoxW,
        Image.asset('mine_vip_next.png'.mineImgPath, width: 14),
        16.sizeBoxW,
      ],
    );
  }

  _buildNormal() {
    String text = '开通会员 享专属特权';
    return Row(
      children: [
        90.sizeBoxW,
        Expanded(
          child: Text(
            text,
            style: TextStyle(color: Colors.white, fontSize: 11, fontWeight: FontWeight.w600),
          ),
        ),
        ShaderMask(
          shaderCallback: (Rect bounds) {
            return LinearGradient(
              colors: [Color(0xffE3B284), Color(0xffFEEEDE)],
              tileMode: TileMode.mirror,
            ).createShader(bounds);
          },
          blendMode: BlendMode.srcATop,
          child: Center(
            child: Text(
              '升级会员',
              style: TextStyle(fontSize: 10),
            ),
          ),
        ),
        4.sizeBoxW,
        Image.asset('mine_vip_next.png'.mineImgPath, width: 14),
        16.sizeBoxW,
      ],
    );
  }

  _buildVip(GlobalStore global) {
    return Row(
      children: [
        88.sizeBoxW,
        Text(
          '${globalStore.meInfo?.vipName == null || globalStore.meInfo?.vipName == '' ? '普通会员' : globalStore.meInfo?.vipName}',
          style: TextStyle(color: Colors.white, fontSize: 11, fontWeight: FontWeight.w600),
        ),
        7.sizeBoxW,
        Expanded(
          child: Text(
            '到期时间 ${DateTimeUtil.utcTurnYear(global.meInfo?.vipExpireDate, char: ".")}',
            style: TextStyle(color: Colors.white, fontSize: 9, fontWeight: FontWeight.w500),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        ShaderMask(
          shaderCallback: (Rect bounds) {
            return LinearGradient(
              colors: [Color(0xffE3B284), Color(0xffFEEEDE)],
              tileMode: TileMode.mirror,
            ).createShader(bounds);
          },
          blendMode: BlendMode.srcATop,
          child: Center(
            child: Text(
              '升级会员',
              style: TextStyle(fontSize: 10),
            ),
          ),
        ),
        3.sizeBoxW,
        Image.asset('mine_vip_next.png'.mineImgPath, width: 14),
        12.sizeBoxW,
      ],
    );
  }
}
